package com.boe.csb.core.common.utils;

import org.apache.commons.lang3.RandomUtils;

import java.util.UUID;

/**
 * MyRandomUtils
 * Created by tianjin.lp on 16/9/4.
 */
public class MyRandomUtils {

    public static Long randomLong() {
        return RandomUtils.nextLong(0, 9999999);
    }

    public static String randomUUID() {
        return toShortString(UUID.randomUUID());
    }

    private static String toShortString(UUID u) {
        return UUIDtoString(u);
    }

    private static String UUIDtoString(UUID u) {
        long mostSigBits = u.getMostSignificantBits();
        long leastSigBits = u.getLeastSignificantBits();
        return (digits(mostSigBits >> 32, 8) + digits(mostSigBits >> 16, 4) + digits(mostSigBits, 4)
                + digits(leastSigBits >> 48, 4) + digits(leastSigBits, 12));
    }

    private static String digits(long val, int digits) {
        long hi = 1L << (digits * 4);
        return Long.toString(hi | (val & (hi - 1)), 36).substring(1);
    }

}
